package com.sanguo.dao;

import com.sanguo.entity.General;
import com.sanguo.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GeneralDAO {
    public List<General> getGenerals(int page, int pageSize, String sortField) {
        List<General> generals = new ArrayList<>();
        String sql = "SELECT * FROM (SELECT a.*, ROWNUM rn FROM (SELECT * FROM generals ORDER BY " + sortField + ") a WHERE ROWNUM <= ?) WHERE rn > ?";
        
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            int start = (page - 1) * pageSize;
            pstmt.setInt(1, page * pageSize);
            pstmt.setInt(2, start);
            
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    General general = new General();
                    general.setId(rs.getInt("id"));
                    general.setName(rs.getString("name"));
                    general.setStrategy(rs.getInt("strategy"));
                    general.setForce(rs.getInt("force"));
                    general.setAgility(rs.getInt("agility"));
                    generals.add(general);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return generals;
    }

    public int getTotalGenerals() {
        String sql = "SELECT COUNT(*) FROM generals";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public General getGeneralById(int id) {
        String sql = "SELECT * FROM generals WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    General general = new General();
                    general.setId(rs.getInt("id"));
                    general.setName(rs.getString("name"));
                    general.setStrategy(rs.getInt("strategy"));
                    general.setForce(rs.getInt("force"));
                    general.setAgility(rs.getInt("agility"));
                    return general;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void addGeneral(General general) throws SQLException {
        String sql = "INSERT INTO generals (id, name, strategy, force, agility) VALUES (generals_seq.NEXTVAL, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, general.getName());
            pstmt.setInt(2, general.getStrategy());
            pstmt.setInt(3, general.getForce());
            pstmt.setInt(4, general.getAgility());
            int rows = pstmt.executeUpdate();
            if (rows != 1) {
                throw new SQLException("添加武将失败");
            }
        }
    }

    public void updateGeneral(General general) {
        String sql = "UPDATE generals SET name = ?, strategy = ?, force = ?, agility = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, general.getName());
            pstmt.setInt(2, general.getStrategy());
            pstmt.setInt(3, general.getForce());
            pstmt.setInt(4, general.getAgility());
            pstmt.setInt(5, general.getId());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteGeneral(int id) {
        String sql = "DELETE FROM generals WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
} 